#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct stack{
    int size;
    int top;            //top is the next num coming in!
    unsigned int *p;
}stack;

stack* stack_init(int large)
{
    stack *mem = calloc(1, sizeof(stack));
    if(mem == NULL)
    {
        perror("stack mem calloc faile");
        return NULL;
    }
    mem->top = 0;
    mem->size = large;
    mem->p = calloc(1, large);
    if(mem->p == NULL)
    {
        perror("stack p calloc fail");
        free(mem);
        mem = NULL;
        return NULL;
    }
    return mem;
}

bool stack_full(stack *p_satck)
{
    return p_satck->top == p_satck->size;       //if it equals!
}

void stack_push(stack *p_stack, unsigned int data)
{
    // printf("333\n");
    p_stack->p[p_stack->top] = data;
    // printf("p_stack->p[%d]=%d\n", p_stack->top, p_stack->p[p_stack->top]);
    p_stack->top++;
}
unsigned int stack_pull(stack *p_stack)
{
    p_stack->top--;
    // printf("p_stack->p[%d]=%d\n", p_stack->top, p_stack->p[p_stack->top]);
    return p_stack->p[p_stack->top];
}

int main(void)
{
    unsigned int num = 0;
    unsigned int quotient = 0;
    unsigned int remainder = 0;

    stack *p_stack = stack_init(400);
    printf("please input a decimal unsigned int number:\n");
    scanf("%u", &num);

    quotient = num;
    while(quotient != 0)
    {
        remainder = quotient % 2;
        stack_push(p_stack, remainder);
        quotient /= 2;
        printf("remainder=%u, quotient=%u, top=%d\n", remainder, quotient, p_stack->top);
    }
    while(p_stack->top != 0)
    {
        printf("%u", stack_pull(p_stack));
    }
    printf("\n");
    return 0;

}